﻿package src {
	
	import flash.display.CapsStyle;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import src.uipackage.ui.OBO_ColorPicker;
	import src.uipackage.ui.events.ColorEvent;
	
	/**
	 * A Tab object to represent the tab with track legends
	 * @author Indrek Tamming
	 */
	public class ConservationTab extends Tab {
		
		/**
		 * Callback to the Viewer class (base class)
		 */
		private var cb:Viewer;
		
		/**
		 * An array of Objects with track parameters
		 */
		private var conservationParams:Array;
		
		/**
		 * Total number of tracks
		 */
		private var noOfTracks:int;
		
		/**
		 * Init the track legend tab
		 * @param	cb
		 */
		public function ConservationTab(cb:Viewer):void {
			this.cb = cb;
			
			conservationParams = cb.conservationParams;
			noOfTracks = conservationParams.length;
		}
		
		/**
		 * Draw and init the tab UI
		 */
		public function draw():void {
			while (numChildren > 0) removeChildAt(0);
			
			for (var i:int = 0; i < noOfTracks; i++) {
				var params:Object = conservationParams[i];
				var color:uint = uint(params.color);
				
				addChild(drawLabel("TRACK " + String(i + 1), -5, i * 100, true, 12));
				
				var legend:Sprite = drawLegend(cb.zoomLevel, color, Number(params.maximum), String(params.style));
				legend.y = i * 100 + 40;
				addChild(legend);
				
				var showHideBtn:MovieClip = new ShowHideBtn();
				showHideBtn.params = params;
				showHideBtn.y = i * 100 + 10;
				showHideBtn.x = 115;
				showHideBtn.buttonMode = true;
				if (Boolean(params.hidden)) showHideBtn.gotoAndStop("Hide");
				else showHideBtn.gotoAndStop("Show");
				showHideBtn.addEventListener(MouseEvent.CLICK, showHideBtnClicked);
				addChild(showHideBtn);
				
				var colorPicker:OBO_ColorPicker = new OBO_ColorPicker(color, 1);
				colorPicker.name = String(i);
				colorPicker.x = 115;
				colorPicker.y = i * 100 + 22;
				colorPicker.addEventListener(ColorEvent.COLOR_SELECT, colorSelect);
				addChild(colorPicker);
			}
		}
		
		/**
		 * Show/hide button is clicked
		 * @param	e
		 */
		private function showHideBtnClicked(e:MouseEvent):void {
			var btn:MovieClip = e.target as MovieClip;
			var params:Object = Object(btn.params);
			if (btn.currentLabel == "Show") {
				btn.gotoAndStop("Hide");
				params.hidden = true;
			}
			else {
				btn.gotoAndStop("Show");
				params.hidden = false;
			}
			cb.uiIsDirty = true;
			cb.viewIsDirty = true;
		}
		
		/**
		 * The color is being selected
		 * @param	e
		 */
		private function colorSelect(e:ColorEvent):void {
			conservationParams[int(e.target.name)].color = e.color;
			cb.uiIsDirty = true;
			cb.viewIsDirty = true;
		}
		
		/**
		 * Draw a track legend
		 * @param	zoomLevel	-	zoom level
		 * @param	color		-	track color
		 * @param	max			-	track maximum
		 * @param	style		-	upper, lower or center
		 * @return	-	 a Sprite with legend on
		 */
		public function drawLegend(zoomLevel:int, color:uint, max:Number, style:String):Sprite {
			var sp:Sprite = new Sprite();
			
			if (max < 1) max = 1; // TODO remove?
			var _xIncr:Number = cb.getLetterSize(zoomLevel);
			var heightMultiplier:Number = 14;
			var _x:Number = 0; 
			var _value:Number = max;
			var _incr = max / (100/_xIncr);
			var g:Graphics = sp.graphics;
			var maxHeight:Number;
			if(style == "center") {
				while (_x < 100) {
					g.beginFill(color, _value);
					g.drawRect(_x, -1 * _value * heightMultiplier, _xIncr, 2 * _value * heightMultiplier);
					g.endFill();
					_value -= _incr;
					_x += _xIncr;
				}
				maxHeight = max * heightMultiplier;
				g.lineStyle(1);
				g.moveTo(0, -maxHeight);
				g.lineTo(0, maxHeight);
				g.moveTo(0, 0);
				g.lineTo(110, 0);
				sp.addChild(drawLabel(String(max), -8, -maxHeight - 4, true, 8));
				sp.addChild(drawLabel("0", -8, -4, true, 8));
				sp.addChild(drawLabel(String(max), -8, maxHeight - 4, true, 8));
			}
			else {
				while (_x < 100) {
					g.beginFill(color, _value * 0.33);
					g.drawRect(_x, (-1) * (_value * heightMultiplier), _xIncr, _value * heightMultiplier);
					g.endFill();
					g.lineStyle(1, color, 1, true, "normal", CapsStyle.ROUND);
					g.moveTo(_x, ( -1) * (_value * heightMultiplier));
					g.lineTo(_x + _xIncr, ( -1) * (_value * heightMultiplier));
					g.lineStyle();
					_value -= _incr;
					_x += _xIncr;
				}
				maxHeight = max * heightMultiplier;
				g.lineStyle(1);
				g.moveTo(0, -maxHeight);
				g.lineTo(0, 0);
				g.lineTo(110, 0);
				sp.addChild(drawLabel("0", -8, -4, true, 8));
				sp.addChild(drawLabel(String(max), -8, -maxHeight - 4, true, 8));
			}
			
			
			
			return sp;
		}
		
		/**
		 * Deprecated
		 * @param	zoomLevel
		 * @return
		 */
		public function drawConservationLegend(zoomLevel:int):Sprite {
			var s:Sprite = new Sprite();
			var g:Graphics = s.graphics;
			
			var _xIncr:Number;
			var heightMultiplier:Number;
			switch(zoomLevel) {
				case 1:
					heightMultiplier = 14;
					_xIncr = 10;
					break;
				case 2:
					heightMultiplier = 12;
					_xIncr = 5;
					break;
				case 3:
					heightMultiplier = 10;
					_xIncr = 1;
					break;
				case 4:
					heightMultiplier = 8;
					_xIncr = 1;
					break;
				default: break;
			}
			var _x:Number = 0; 
			var _value:Number = 0;
			var _incr = cb.conservationParams.maximum / (100/_xIncr);
			while (_x < 100) {
				_value += _incr;
				g.beginFill(cb.conservationParams.color, _value);
				g.drawRect(_x, ( -1) * _value * heightMultiplier, _xIncr, 2 * _value * heightMultiplier);
				g.endFill();
				_x += _xIncr;
			}
			var maxHeight:Number = cb.conservationParams.maximum * heightMultiplier;
			g.lineStyle(1,0x333333);
			g.moveTo( -10, -maxHeight);
			g.lineTo( 100, -maxHeight);
			g.moveTo( -10, 0);
			g.lineTo(100, 0);
			g.moveTo( -10, maxHeight);
			g.lineTo( 100, maxHeight);
			g.moveTo(0, -maxHeight - 10);
			g.lineTo(0, maxHeight + 10);
			s.addChild(drawLabel(String(cb.conservationParams.maximum), -9, -maxHeight - 10, true, 7));
			s.addChild(drawLabel("0", -9, - 10, true, 7));
			s.addChild(drawLabel(String(cb.conservationParams.maximum), -9, maxHeight - 10, true, 7));
			
			return s;
		}
		
	}
	
}